/* 
const obj = {
    data: [
        ['xiaoming', 'male', '18', 'beijing', '2020-01-02'],
        ['xiaofang', 'female', '18', 'shanghai', '2020-03-02']
    ],
    columns: [
        { name: 'name', note: '' },
        { name: 'gender', note: '' },
        { name: 'age', note: '' },
        { name: 'address', note: '' },
        { name: 'registerTime', note: '' },
    ]
};
输出下面结果 
[
    { name: 'xiaoming', 'gender': 'male', age: '18', address: 'beijing', registerTime: '2020-01-02' },
    { name: 'xiaofang', 'gender': 'female', age: '18', address: 'shanghai', registerTime: '2020-03-02' }
]
*/

/* 
// 方案一：
const combine = function combine(obj) {
    let { columns, data } = obj,
        columnsKeys = {};
    // 遍历columns，把其变为 {name:0,gender:1,age:2,...} 这样的对象
    columns.forEach((item, index) => columnsKeys[item.name] = index);
    // 遍历data中每一项数据
    return data.map(item => {
        let obj = {};
        // 迭代对象属性，拼接为想要的新对象
        Reflect.ownKeys(columnsKeys).forEach(key => {
            obj[key] = item[columnsKeys[key]];
        });
        return obj;
    });
}; 
*/

// 方案二：
const combine = function combine(obj) {
    let { columns, data } = obj;
    // 把columns中的name属性值组成新数组
    columns = columns.map(item => item.name);
    // 遍历data中每一项数据
    return data.map(item => {
        let obj = {},
            n = -1;
        // 基于递归方式组合为自己想要的对象
        const recur = () => {
            n++;
            if (n >= columns.length) return;
            obj[columns[n]] = item[n];
            recur();
        };
        recur();
        return obj;
    });
}; 

const obj = {
    data: [
        ['xiaoming', 'male', '18', 'beijing', '2020-01-02'],
        ['xiaofang', 'female', '18', 'shanghai', '2020-03-02']
    ],
    columns: [
        { name: 'name', note: '' },
        { name: 'gender', note: '' },
        { name: 'age', note: '' },
        { name: 'address', note: '' },
        { name: 'registerTime', note: '' },
    ]
};
console.log(combine(obj));
